package com.ogawa.a7517.fragment;

import android.view.View;
import android.widget.ImageView;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;

public class MassageSkillFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivKnead, ivZhiya, ivKnock, ivClap, ivTuina, ivSwedish;
    private int[] levImgRes = {R.drawable.btn_gaoji_zt1, R.drawable.btn_gaoji_zt2};

    @Override
    public int getLayoutId() {
        return R.layout.fragment_massage_skill;
    }

    @Override
    public void initView(View view) {
        ivKnead = view.findViewById(R.id.iv_knead_lev);
        ivZhiya = view.findViewById(R.id.iv_zhiya_lev);
        ivKnock = view.findViewById(R.id.iv_knock_lev);
        ivClap = view.findViewById(R.id.iv_clap_lev);
        ivTuina = view.findViewById(R.id.iv_tuina_lev);
        ivSwedish = view.findViewById(R.id.iv_swedish_lev);
        view.findViewById(R.id.tv_knead).setOnClickListener(this);
        view.findViewById(R.id.tv_zhiya).setOnClickListener(this);
        view.findViewById(R.id.tv_knock).setOnClickListener(this);
        view.findViewById(R.id.tv_clap).setOnClickListener(this);
        view.findViewById(R.id.tv_tuina).setOnClickListener(this);
        view.findViewById(R.id.tv_swedish).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_knead) {
            activity7517.publish(Constant.XYZ_HAND_TYPE, "1");
        } else if (id == R.id.tv_zhiya) {
            activity7517.publish(Constant.KNOCK_HAND_TYPE, "4");
        } else if (id == R.id.tv_knock) {
            activity7517.publish(Constant.KNOCK_HAND_TYPE, "7");
        } else if (id == R.id.tv_clap) {
            activity7517.publish(Constant.KNOCK_HAND_TYPE, "1");
        } else if (id == R.id.tv_tuina) {
            activity7517.publish(Constant.XYZ_HAND_TYPE, "4");
        } else if (id == R.id.tv_swedish) {
            activity7517.publish(Constant.XYZ_HAND_TYPE, "6");
        }
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        if (!isInit) {
            return;
        }
        DeviceStateBean statusBean = MassageArmchair.getInstance().getDeviceStatusBean();
        int runningStatus = MassageArmchair.getInstance().getRunningStatus();
        if (statusBean != null) {
            DeviceStateBean.FunctionsBean.XyzHandTypeBean xyzHandType = statusBean.getFunctions().getXyzHandType();
            if (xyzHandType != null) {
                int type = Integer.parseInt(xyzHandType.getModelValue());
                ivKnead.setVisibility(type == 2 || type == 3 ? View.VISIBLE : View.GONE);
                ivTuina.setVisibility(type == 5 ? View.VISIBLE : View.GONE);
                ivSwedish.setVisibility(type == 7 || type == 8 ? View.VISIBLE : View.GONE);
                switch (type) {
                    case 2:
                        ivKnead.setImageResource(levImgRes[0]);
                        break;
                    case 3:
                        ivKnead.setImageResource(levImgRes[1]);
                        break;
                    case 7:
                        ivSwedish.setImageResource(levImgRes[0]);
                        break;
                    case 8:
                        ivSwedish.setImageResource(levImgRes[1]);
                        break;
                }
            }
            DeviceStateBean.FunctionsBean.KnockHandTypeBean knockHandType = statusBean.getFunctions().getKnockHandType();
            if (knockHandType != null) {
                int knockType = Integer.parseInt(knockHandType.getModelValue());
                ivClap.setVisibility(knockType == 2 || knockType == 3 ? View.VISIBLE : View.GONE);
                ivZhiya.setVisibility(knockType == 5 || knockType == 6 ? View.VISIBLE : View.GONE);
                ivKnock.setVisibility(knockType == 8 || knockType == 9 ? View.VISIBLE : View.GONE);
                switch (knockType) {
                    case 2:
                        ivClap.setImageResource(levImgRes[0]);
                        break;
                    case 3:
                        ivClap.setImageResource(levImgRes[1]);
                        break;
                    case 5:
                        ivZhiya.setImageResource(levImgRes[0]);
                        break;
                    case 6:
                        ivZhiya.setImageResource(levImgRes[1]);
                        break;
                    case 8:
                        ivKnock.setImageResource(levImgRes[1]);
                        break;
                    case 9:
                        ivKnock.setImageResource(levImgRes[0]);
                        break;
                }
            }
        }else {
            stateReset();
        }
        if (runningStatus==0){
            stateReset();
        }
    }

    private void stateReset() {
        ivKnead.setVisibility(View.GONE);
        ivZhiya.setVisibility(View.GONE);
        ivKnock.setVisibility(View.GONE);
        ivClap.setVisibility(View.GONE);
        ivTuina.setVisibility(View.GONE);
        ivSwedish.setVisibility(View.GONE);
    }
}
